<?php

namespace app\factory\logic;

use app\common\exception\LogicException;
use app\model\Pig;
use app\model\PigWeight;
use think\facade\Db;

class PigWeightLogic
{
    protected PigWeight $model;
    protected int $factory_id;

    public function __construct()
    {
        $this->model = new PigWeight();
        $this->factory_id = request()->factory_info['factory_id'];
    }

    /**
     * @Description 新增/编辑
     * @author: zj
     * @param int $Id
     * @param int $PigId
     * @param array $param
     * @time 2024/03/18 16:14:35
     * @return true
     */
    public function addEdit(int $Id,int $PigId,array $param)
    {
        Db::startTrans();
        try {
            $exist = $this->model->checkOutIdIsExist($this->factory_id,$Id);
            $pigUpdateData = [];
            if ($exist) {
                $rule = [
                    'Type' => 'integer',
                    'Weight' => 'float|>:0',
                    'GainWeight' => 'float',
                ];
                validate($rule)->check($param);

                $updateData = [];
                if (isset($param['Type'])) {
                    $updateData['type'] = $param['Type'];
                }
                if ($param['Weight']) {
                    $updateData['weight'] = $param['Weight'];
                    $pigUpdateData['weight'] = $param['Weight'];
                }
                if (isset($param['GainWeight'])) {
                    $updateData['gain_weight'] = $param['GainWeight'];
                }
                $updateData['update_record_time'] = $param['RecordTime'];
                $updateData['update_time'] = request()->time();

                $this->model->where('out_id',$Id)
                    ->where('delete_time',$this->model::IS_DELETE_NO)
                    ->update($updateData);
            } else {
                $rule = [
                    'Type' => 'require|integer',
                    'Weight' => 'require|float|>:0',
                    'GainWeight' => 'require|float',
                ];
                validate($rule)->check($param);

                $this->model->insert([
                    'out_id' => $Id,
                    'factory_id'=> $this->factory_id,
                    'out_pig_id' => $PigId,
                    'type' => $param['Type'],
                    'weight' => $param['Weight'],
                    'gain_weight' => $param['GainWeight'],
                    'record_time' => $param['RecordTime'],
                    'update_record_time' => $param['RecordTime'],
                    'create_time' => request()->time(),
                    'update_time' => request()->time()
                ]);
                $pigUpdateData['weight'] = $param['Weight'];
            }
            
            if ($pigUpdateData) { // 更新生猪的体重
                $pigUpdateData['update_record_time'] = $param['RecordTime'];
                $pigUpdateData['update_time'] = request()->time();
                
                $pigModel = new Pig();
                $pigModel->where('out_id', $PigId)
                    ->where('delete_time', $pigModel::IS_DELETE_NO)
                    ->update($pigUpdateData);
            }

            Db::commit();
        }catch (\Exception $exception) {
            Db::rollback();
            throw new LogicException($exception->getMessage());
        }

        return true;
    }

}